﻿
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YDModel;
using YDModel.Entity;
using YDServices;

namespace YDWin
{
    /// <summary>
    /// 存放全局变量的静态类。
    /// </summary>
    public static class Global
    {
        #region 静态成员变量

        /// <summary>
        /// 当前登录用户。  
        /// </summary>
        private static T_User currUser = new T_User();//{ ID = 4, UserCode = "admin", UserName = "管理员" };

        //private static List<getRoleRight_Result> crrrList = null;

        #endregion

        #region 服务器时间

        /// <summary>
        /// 对应Sql服务器日期时间。
        /// </summary>
        private static DateTime _sqlStartTime = DateTime.MinValue;

        /// <summary>
        /// 本地时间。
        /// </summary>
        private static DateTime _localStartTime = DateTime.MinValue;

        /// <summary>
        /// 获取数据库服务器时间。
        /// </summary>
        public static DateTime SqlNow
        {
            get
            {
                if (_sqlStartTime == DateTime.MinValue)  //尚未获取时间，记录Sql和当地时间。
                {
                    BaseService svr = new BaseService();
                    _sqlStartTime = svr.GetSqlNow();
                    if (svr.HasError)
                    {
                        throw new ApplicationException("获取服务器时间失败，系统无法正常工作。" + svr.Errors); 
                    }
                    _localStartTime = DateTime.Now;

                }
                DateTime sqlNow = _sqlStartTime.Add(DateTime.Now - _localStartTime);  //起始Sql时间+（本地流逝时间）=目前Sql时间。
                return sqlNow;
            }
        }

        #endregion

       

        /// <summary>
        /// 获取当前登录的用户
        /// </summary>
        public static T_User User
        {
            get
            {
                return currUser;
            }
            set
            {
                currUser = value; 
            }
        }

        public static List<T_Permission> UserPerList;

        /// <summary>
        /// 主窗体的句柄
        /// </summary>
        public static IntPtr MainHandle { get; set; }


        public static void GirdViewSet(GridView  gridView)
        {
            //设置成一次选择一行 
            gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            gridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click;//单击选择行 不能位click,否则button不能一次点击触发
            gridView.OptionsSelection.EnableAppearanceFocusedCell = false;
            gridView.OptionsBehavior.Editable = false;//不能被编辑
            //列头禁止移动
            gridView.OptionsCustomization.AllowColumnMoving = false;
            //列头禁止排序
            gridView.OptionsCustomization.AllowSort = false;
            //隔行换色
            gridView.Appearance.OddRow.BackColor = Color.White;  // 设置奇数行颜色 // 默认也是白色 可以省略 
            gridView.OptionsView.EnableAppearanceOddRow = true;   // 使能 // 和和上面绑定 同时使用有效 
            gridView.Appearance.EvenRow.BackColor = Color.WhiteSmoke; // 设置偶数行颜色 
            gridView.OptionsView.EnableAppearanceEvenRow = true;   // 使能 // 和和上面绑定 同时使用有效
            //禁止使用过滤器
            gridView.OptionsCustomization.AllowFilter = false;
            //禁止显示表头
            gridView.OptionsView.ShowGroupPanel = false;
            //禁止单击列弹出右键菜单
            gridView.OptionsMenu.EnableColumnMenu = false;
            //行高
            gridView.RowHeight = 28;
            //行号列宽度
            gridView.IndicatorWidth = 30;
            //设置横向滚动条
            //gridView.OptionsView.ColumnAutoWidth = false;
            gridView.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            gridView.HorzScrollVisibility = ScrollVisibility.Always;
            //设置表头高
            gridView.ColumnPanelRowHeight = 50;
            //行中值居中
            //gridView.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            //列标题居中
            gridView.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
        }

    }
}
